<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>SmartPing Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="css/font-awesome.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="css/pages/dashboard.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>
<div class="navbar navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container">
      <a class="brand" href="index.html">SmartPing Dashbord </a>
      <div class="nav-collapse">
        <ul class="nav pull-right">
          <li></li>
        </ul>
      </div>
      <!--/.nav-collapse -->
    </div>
    <!-- /container -->
  </div>
  <!-- /navbar-inner -->
</div>
<!-- /navbar -->
<div class="subnavbar">
  <div class="subnavbar-inner">
    <div class="container">
      <ul class="mainnav">
        <li><a href="index.html"><i class="icon-mail-forward"></i><span>Forward Ping</span> </a> </li>
        <li><a href="reverse.html"><i class="icon-mail-reply"></i><span>Reverse Ping</span> </a> </li>
        <li class="active"><a href="topology.html"><i class="icon-bar-chart"></i><span>Topology</span> </a> </li>
        <li><a href="alerts.html"><i class="icon-warning-sign"></i><span>Alerts</span> </a> </li>
        <!--<li><a href="tools.html"><i class="icon-wrench"></i><span>Test Tools</span> </a> </li>-->
        <li><a href="config.html"><i class="icon-cog"></i><span>Config</span> </a> </li>
      </ul>
    </div>
    <!-- /container --> 
  </div>
  <!-- /subnavbar-inner --> 
</div>
<!-- /subnavbar -->
<div class="main" style="margin-top:-20px;">
  <div class="main-inner">
    <div class="container">
      <div class="row">
        <div class="span10">
          <div id="echarts" style="width:100%;"></div>
        </div>
        <!-- /span10 -->
        <div class="span2">
          <div class="widget widget-table action-table">
            <div class="widget-header"> <i class="icon-th-list"></i>
              <h3>Agent List</h3>
            </div>
            <!-- /widget-header -->
            <div class="widget-content">
              <table class="table table-striped table-bordered">
                <thead>
                <tr>
                  <th> Agent </th>
                </tr>
                </thead>
                <tbody class="agentlist">

                </tbody>
              </table>
            </div>
            <!-- /widget-content -->
          </div>
        </div>
      </div>
    </div>
    <!-- /container --> 
  </div>
  <!-- /main-inner --> 
</div>
<!-- /main -->

<div class="footer">
  <div class="footer-inner">
    <div class="container">
      <div class="row">
        <div class="span12"> &copy; 2017 <a target="_blank" href="http://smartping.org" >SmartPing.org</a> <span style="float:right" id="verion"></span></div>
        <!-- /span12 --> 
      </div>
      <!-- /row --> 
    </div>
    <!-- /container --> 
  </div>
  <!-- /footer-inner --> 
</div>
<!-- /footer --> 
<!-- Le javascript
================================================== --> 
<!-- Placed at the end of the document so the pages load faster --> 
<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/echarts.min.js"></script>
<script>
    function Refresh()
    {
        window.location.reload();
    }
    $(function(){
        $("#echarts").height($(window).height()-220);

        $.getJSON("/api/config.json",function(result){
            $("#verion").html("Version: "+result["Ver"])
            $.each(result["Targets"], function(i, field){
                if(field.Type=="CS") {
                    if(field.Addr==result["Ip"]){
                        return true;
                    }
                    $(".agentlist").append("<tr><td><i class='icon-spinner icon-spin animated alerticon-" + field.Name + "'></i>&nbsp;<a target='_blank' href='http://" + field.Addr + ":" + result["Port"] + "/topology.html'>" + field.Name + "</a></td></tr>");
                }
            });
            setTimeout('Refresh()',result["Alertcycle"]*60*1000);
            //var topodata={}
            var dataarea = [];
            var dataline = [];
            option = {
                tooltip: {
                    show: true
                },
                animationDurationUpdate: 1500,
                animationEasingUpdate: 'quinticInOut',
                series : [
                    {
                        type: 'graph',
                        layout: 'circular',
                        symbolSize: result["Tsymbolsize"],
                        focusNodeAdjacency:true,
                        roam: true,
                        label: {
                            normal: {
                                show: true
                            }
                        },
                        edgeSymbol: ['circle', 'arrow'],
                        edgeSymbolSize: [3, 15],
                        edgeLabel: {
                            normal: {
                                textStyle: {
                                    fontSize: 15
                                }
                            }
                        },
                        data : dataarea,
                        links: dataline,
                        lineStyle: {
                            normal: {
                                opacity: 0.9,
                                width: result["Tline"],
                                curveness: 0
                            }
                        }
                    }
                ]
            };
            var myChart = echarts.init(document.getElementById('echarts'));
            myChart.setOption(option);

            $.each(result["Targets"], function(i, field){
                if(field.Type=="CS") {
                    $.ajax({
                        dataType: "json",
                        url: 'http://' + field.Addr + ':' + result['Port'] + '/api/topology.json',
                        //data: topodata,
                        success: function (topodata) {
                            $(".alerticon-" + field.Name + "").remove();
                            for (var td in topodata) {
                                if (topodata[td] == "true") {
                                    mcurveness = 0;
                                    mcolor = "green"
                                } else {
                                    if ($("#alert").length < 1) {
                                        $(".main").append('<audio id="alert" style="display:none"  autoplay="autoplay"  controls="controls" loop="loop"><source src="' + result["Alertsound"] + '" type="audio/mp3"  /></audio>');
                                    }
                                    mcurveness = 0.2;
                                    mcolor = "red"
                                }
                                dataline.push({
                                    source: field.Name,
                                    target: td,
                                    lineStyle: {
                                        normal: {curveness: mcurveness, color: mcolor}
                                    }
                                })
                            }
                            dataarea.push({
                                name: field.Name,
                                draggable: "true",
                                itemStyle: {
                                    normal: {
                                        color: "green"
                                    }
                                }
                            });
                            myChart.setOption(option);
                        },
                        timeout: result["Timeout"] * 1000
                    }).fail(function (xhr, status) {
                        $(".alerticon-" + field.Name + "").removeClass("icon-spinner").removeClass("icon-spin").removeClass("animated");
                        $(".alerticon-" + field.Name + "").addClass("icon-warning-sign");
                        dataarea.push({
                            name: field.Name,
                            draggable: "true",
                            itemStyle: {
                                normal: {
                                    color: "red"
                                }
                            }
                        });
                        myChart.setOption(option);
                        if ($("#alert").length < 1) {
                            $(".main").append('<audio id="alert" style="display:none"  autoplay="autoplay"  controls="controls" loop="loop"><source src="' + result["Alertsound"] + '" type="audio/mp3"  /></audio>');
                        }
                    });
                }else{
                    dataarea.push({
                        name: field.Name,
                        draggable: "true",
                        itemStyle: {
                            normal: {
                                color: "green"
                            }
                        }
                    });
                    myChart.setOption(option);
                }
            });
        });
    });
</script>
</body>
</html>
